﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeMei.Common;

public partial class Manager_download_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserInfo"] == null)
        {
            Response.Redirect(ConfigHelper.SystemUrl + "Manager/login.aspx", true);

        }
        if (!IsPostBack)
        {

            if (Request["ID"] != null)
            {
                this.InitModel(int.Parse(Request["ID"].ToString()));
                FileUpload1.Required = false;
            }
        }
    }

    /// <summary>
    /// 绑定实体类到控件
    /// </summary>
    protected void InitModel(int ID)
    {
        KeMei.Model.Download model = null;
        KeMei.BLL.Download bll = new KeMei.BLL.Download();

        try
        {
            model = bll.GetModel(ID);

            if (model != null)
            {
                txtName.Text = model.TName;
                txtdesc.Text = model.FileDes;
                if (!string.IsNullOrEmpty(model.FileUrl))
                {
                    hfFileUrl.Text = model.FileUrl;

                }
            }
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("初始化实体类失败：" + ex.Message);
        }
    }

    protected void SaveFile(out string filePath)
    {
        bool fileOK = false;
        filePath = string.Empty;

        if (!(FileUpload1.ShortFileName.Length > 0) && Request["ID"] != null)
        {
            filePath = hfFileUrl.Text;
            return;

        }
        if (!string.IsNullOrEmpty(hfFileUrl.Text.Trim()))
        {
            FileHelper.DeleteFile(Server.MapPath(ConfigHelper.SystemUrl + hfFileUrl.Text));
        }

        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
        String[] allowedExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".rar", ".zip", ".doc", ".docx", ".xsl", ".xslx" };
        foreach (string ex in allowedExtensions)
        {
            if (ex == fileExtension)
            {
                fileOK = true;
                break;
            }
        }



        if (!fileOK)
        {
            ExtAspNet.Alert.Show("请选择正确的文件格式");
            return;
        }
        string fileName = FileUpload1.ShortFileName;
        fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
        fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath(ConfigHelper.SystemUrl + "upload/" + fileName));
            filePath = "upload/" + fileName;
        }


    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        KeMei.BLL.Download bll = new KeMei.BLL.Download();
        KeMei.Model.Download model = new KeMei.Model.Download();

        try
        {
            model.TName = txtName.Text;
            model.FileDes = txtdesc.Text;
            model.Recorddate = DateTime.Now;
            model.lmID = 3;

            string filePath = string.Empty;
            this.SaveFile(out filePath);
            model.FileUrl = filePath;

            if (Request["ID"] != null)
            {
                model.ID = int.Parse(Request["ID"].ToString());

                bll.Update(model);
            }
            else
            {
                bll.Add(model);
            }
            ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
        catch (Exception ex)
        {
            ExtAspNet.Alert.Show("保存失败：" + ex.Message);
        }
    }
}